import { message } from 'ant-design-vue'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

import { algoDevApi } from '@/services/api'
import { downloadFile } from '@/utils'

export interface FileInfo {
  id: string
  /**
   * 层级位置
   */
  location: string
  /**
   * 文件名
   */
  name: string
  /**
   * 状态 0-准备中  1-准备成功  2-准备失败
   */
  status: number
  /**
   * 状态名称
   */
  statusName: string
  /**
   * 访问地址
   */
  downloadUrl: string
  /**
   * 访问地址
   */
  url?: string
}

export const useExportListDrawer = defineStore('exportListDrawer', () => {
  const open = ref(false)
  const downList = ref<FileInfo[]>()
  // 间隔
  const interval = 3000
  let timer: any = null

  const changeOpen = (bool: boolean) => {
    open.value = bool
  }

  const openDrawer = () => {
    open.value = true
    getSyncDownList()
    timer = setInterval(() => {
      getSyncDownList()
    }, interval)
  }

  const closeDrawer = () => {
    open.value = false
    clearInterval(timer)
  }

  /**
   * 添加下载任务
   */
  const addDownTask = async (id: string | number) => {
    try {
      await algoDevApi.export({}, { isAddUid: true, urlParams: { id } })
      message.success('成功添加至任务队列')
      openDrawer()
    } catch (error) {}
  }

  /**
   * 获取下载列表
   */
  const getDownList = computed(() => {
    return downList.value
  })

  /**
   * 总数
   */
  const getTotalCount = computed(() => {
    return downList.value ? downList.value.length : 0
  })

  /**
   * 获取失败列表
   */
  const getFailedList = computed(() => {
    return downList.value?.filter(item => item.status === 2)
  })

  /**
   * 获取下载任务列表
   */
  const getSyncDownList = async () => {
    const { data }: { data: any[] } = await algoDevApi.getExportList()
    downList.value = data.map(item => {
      return {
        id: item.exportId,
        name: item.exportName,
        status: item.state,
        location: `算法管理/${item.description}`,
        statusName: '',
        downloadUrl: item.filePath
      }
    })
  }

  /**
   * 移除单个任务
   * @param fileInfo
   */
  const removeTask = async (fileInfo: FileInfo) => {
    try {
      await algoDevApi.cancelExport({}, { isAddUid: true, urlParams: { id: fileInfo.id } })
      downList.value = downList.value?.filter(item => item.id !== fileInfo.id)
      message.success('删除成功')
    } catch (error) {
      console.log(error)
    }
  }

  /**
   * 清空全部任务
   */
  const removeAllTask = async () => {
    try {
      if (!downList.value || downList.value?.length === 0) {
        return
      }
      await algoDevApi.cancelAllExport()
      downList.value = downList.value?.filter(item => item.status === 0)
    } catch (error) {
      console.log(error)
    }
  }

  /**
   * 下载
   */
  const download = async (fileInfo: FileInfo) => {
    const { data } = await algoDevApi.downFile({}, { urlParams: { id: fileInfo.id } })
    downloadFile(data.fileUrl)
  }

  return {
    open,
    downList,
    changeOpen,
    openDrawer,
    closeDrawer,
    getDownList,
    getFailedList,
    getSyncDownList,
    addDownTask,
    removeTask,
    removeAllTask,
    download,
    getTotalCount
  }
})
